﻿
//就绪函数
$(function () {
    Show()
})
//库位显示
function Show() {
    layui.use(function () {
        var table = layui.table;
        var locationName = $("#locationName").val()
        var locationNumber = $("#locationNumbers").val();
        var locationState = $("#locationState").val()
        //console.log(locationState)
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',//导出
            defaultTolbar:['exports'],//导出
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'locationName', title: '库位名称' },
                { field: 'locationNumber', title: '库位编号' },
                { field: 'locationArea', title: '面积' },
                { field: 'locationweight', title: '最大载重量' },
                { field: 'warehouseID', title: '所属仓库' },
                { field: 'reservoirId', title: '所属库区' },
                {
                    field: 'locationState', title: '状态', templet: function (x) {
                        if (x.locationState == true) {
                            return "启用"
                        } else {
                            return "禁用"
                        }
                    }
                },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.locationId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.locationId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }
                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        var data = {
            pageIndex: 1,
            pageSize: 80,
            locationName: locationName,
            locationNumber: locationNumber,
            locationState: locationState
        }
        $.ajax({

            url: "/Warehouses/LocationShowsj",
            type: "get",
            data: data,
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                console.log(data.data)
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    })
}
//添加
layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;
    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: '库区管理添加',
                content: `
        <form class="layui-form" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">库位名称:</label>
              <div class="layui-input-inline">
                    <input type="text" name="locationName"lay-verify="required"  id="locationName" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">库位编号:</label>
              <div class="layui-input-block">
                <input type="text" name="locationNumber" lay-verify="required" id="locationNumber" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">面积:</label>
              <div class="layui-input-inline">
                    <input type="text" name="locationArea"lay-verify="required"  id="locationArea" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">最大载重量:</label>
              <div class="layui-input-inline">
                    <input type="text" name="locationweight"lay-verify="required"  id="locationweight" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">所属仓库:</label>
              <div class="layui-col-md6">
                <select id="warehouseID" name="warehouseID" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                      <option value="">请选择</option>
                      <option value="仓库一">仓库一</option>
                      <option value="仓库二">仓库二</option>
                      <option value="仓库三">仓库三</option>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">所属库区:</label>
              <div class="layui-col-md6">
                <select id="reservoirId" name="reservoirId" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                      <option value="">请选择</option>
                      <option value="库区一">库区一</option>
                      <option value="库区二">库区二</option>
                      <option value="库区三">库区三</option>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">状态:</label>
              <div class="layui-input-inline">
                   <input type="radio" id="locationStates" name="locationState" title="启用" checked>
                   <input type="radio" id="locationState" name="locationState" title="禁用">
              </div>
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                      <textarea placeholder="请输入内容" name="locationRemark" id="locationRemark" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item" style="text-align:center">
                <button class="layui-btn" lay-submit lay-filter="demo-login">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">取消</button>
          </div>
        </form>`,
                area: ['800px', '450px'],
                success: function () {

                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {

                        var field = data.field;
                        //field.locationState = $('#locationState').is(':checked') ? false : true
                        field.locationState = $('#locationStates').is(':checked')
                        console.log(field.locationState)
                        $.ajax({
                            url: '/Warehouses/LocationAdd',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                if (s.code == 1) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });

            // 获取当前日期时间
            var now = new Date();
            var hours = ("0" + now.getHours()).slice(-2);
            var minutes = ("0" + now.getMinutes()).slice(-2);
            var seconds = ("0" + now.getSeconds()).slice(-2);
            var serialNumber = "KW" + hours + minutes + seconds;
            var inputElement = document.getElementById('locationNumber');
            inputElement.value = serialNumber;
            console.log(inputElement.value)
        }
    })
});
//编辑
var obj = {
    locationId:'',
    locationName: '',
    locationNumber: '',
    locationArea: '',
    locationweight: '',
    warehouseID: '',
    reservoirId: '',
    locationState: '',
    locationRemark: '',
    addName: '',
    addTime: ''
}
window.editData = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    //反填库位
    $.ajax({
        url: '/Warehouses/GetLocationId',
        type: 'get',
        data: { Id: id, token: localStorage.getItem("token") },
        success: res => {
            var res = JSON.parse(res)
            console.log(res.data)

            $('#locationId').val(res.data.locationId)
            $('#locationNames').val(res.data.locationName)
            $('#locationNumber').val(res.data.locationNumber)
            $('#locationArea').val(res.data.locationArea)
            $('#locationweight').val(res.data.locationweight)
            $('#warehouseID').val(res.data.warehouseID)
            $('#reservoirId').val(res.data.reservoirId)
            form.render('select')
            if (res.data.locationState == true) {
                $('#locationStat').prop('checked', true)
            } else {
                $('#locationSta').prop('checked', true)
            }
            form.render('radio')
            $('#locationRemark').val(res.data.locationRemark)
            $('#addName').val(res.data.addName)
            $('#addTime').val(res.data.addTime)
            obj.locationId = res.data.locationId
            obj.locationName = res.data.locationName
            obj.locationNumber = res.data.locationNumber
            obj.locationArea = res.data.locationArea
            obj.locationweight = res.data.locationweight
            obj.warehouseID = res.data.warehouseID
            obj.reservoirId = res.data.reload
            obj.locationState = res.data.locationState
            obj.addName = res.data.addName
            obj.addTime = res.data.addTime
        }
    })
    //修改库位
    layer.open({
        type: 1,
        title: '编辑库位',
        content: `<form class="layui-form" action="">
                  <div class="layui-form-item">
                    <div class="layui-inline">
                      <label class="layui-form-label">库位名称:</label>
                      <div class="layui-input-inline">
                            <input type="text" name="locationNames"lay-verify="required"  id="locationNames" autocomplete="off" class="layui-input">
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">库位编号:</label>
                      <div class="layui-input-block">
                        <input type="text" name="locationNumber" lay-verify="required" id="locationNumber" autocomplete="off" class="layui-input">
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">面积:</label>
                      <div class="layui-input-inline">
                            <input type="text" name="locationArea"lay-verify="required"  id="locationArea" autocomplete="off" class="layui-input">
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">最大载重量:</label>
                      <div class="layui-input-inline">
                            <input type="text" name="locationweight"lay-verify="required"  id="locationweight" autocomplete="off" class="layui-input">
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">所属仓库:</label>
                      <div class="layui-col-md6">
                        <select id="warehouseID" name="warehouseID" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                              <option value="">请选择</option>
                              <option value="仓库一">仓库一</option>
                              <option value="仓库二">仓库二</option>
                              <option value="仓库三">仓库三</option>
                        </select>
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">所属库区:</label>
                      <div class="layui-col-md6">
                        <select id="reservoirId" name="reservoirId" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select">
                              <option value="">请选择</option>
                              <option value="库区一">库区一</option>
                              <option value="库区二">库区二</option>
                              <option value="库区三">库区三</option>
                        </select>
                      </div>
                    </div>
                    <div class="layui-inline">
                      <label class="layui-form-label">状态:</label>
                      <div class="layui-input-inline">
                           <input type="radio" id="locationStat" name="locationStates" title="启用">
                           <input type="radio" id="locationSta" name="locationStates" title="禁用">
                      </div>
                    </div>
                  </div>
                  <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                              <textarea placeholder="请输入内容" name="locationRemark" id="locationRemark" class="layui-textarea"></textarea>
                    </div>
                  </div>
                  <div class="layui-form-item" style="text-align:center">
                        <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                  </div>
                </form>`,
        area: ['800px', '450px'],
        success: function () {
            var obj1 = {
                locationId: '',
                locationName: '',
                locationNumber: '',
                locationArea: '',
                locationweight: '',
                warehouseID: '',
                reservoirId: '',
                locationState: '',
                locationRemark: '',
                addName: '',
                addTime: ''
            }
            // 对弹层中的表单进行初始化渲染
            form.render()
            form.on('submit(demo-login)', function (data) {
                var field = data.field
                obj1.locationId = obj.locationId
                obj1.locationName = obj.locationName
                obj1.locationNumber = field.locationNumber
                obj1.locationArea = field.locationArea
                obj1.locationweight = field.locationweight
                obj1.warehouseID = field.warehouseID
                obj1.reservoirId = field.reservoirId
                obj1.locationState = $('#locationStat').is(':checked')
                obj1.locationRemark = field.locationRemark
                obj1.addName = obj.addName
                obj1.addTime = obj.addTime
                $.ajax({
                    url: '/Warehouses/UpdLocation',
                    type: 'post',
                    data: { dto: obj1, token: localStorage.getItem("token") },
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(field)
                        console.log(res)
                        if (res.data >= 1) {
                            layer.alert("修改成功");
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                        else {
                            layer.alert("修改失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            })
        }
    })
}
window.deleteData = function (id) {
    layer.confirm('确定删除该条数据吗？', function (index) {
        $.ajax({
            url: '/Warehouses/DelLocation',
            type: 'post',
            data: { data: JSON.stringify(id), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.code == 1) {
                    layer.alert("删除成功");
                    Show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    })
}